package auction.chapter08;

import java.util.HashSet;
import java.util.Set;

public class ItemFormulaJoin{
	private Long id;
	private String title;
	
	/**
	 * the successful bid
	 */
	private BidFormulaJoin successfulBid;
	
	/**
	 * a list of bids to this item
	 */
	private Set<BidFormulaJoin> bids=new HashSet<BidFormulaJoin>();
	
	public ItemFormulaJoin(){
	}
	
	public ItemFormulaJoin(String t){
		title=t;
	}
	
	public Long getId(){
		return id;
	}
	public void setId(Long i){
		id=i;
	}
	
	public void setTitle(String i){
		title=i;
	}
	public String getTitle(){
		return title;
	}

	public BidFormulaJoin getSuccessfulBid() {
		return successfulBid;
	}

	/**
	 * GOOD!!! BUT will trigger the loading of collection
	 * @param successfulBid
	 */
	public void setSuccessfulBid(BidFormulaJoin successfulBid) {
		if(successfulBid != null){
			for(BidFormulaJoin b : bids){
				b.setSuccessful(false);
			}
			
			successfulBid.setSuccessful(true);
			this.successfulBid=successfulBid;
		}
	}

	public Set<BidFormulaJoin> getBids() {
		return bids;
	}

	public void setBids(Set<BidFormulaJoin> bids) {
		this.bids = bids;
	}
	
	public void addBid(BidFormulaJoin bid){
		this.bids.add(bid);
		bid.setItem(this);
	}
}